package com.geilizu.dom.service.impl;

import com.geilizu.dom.dao.DormitoryDao;
import com.geilizu.dom.dao.StudentDao;
import com.geilizu.dom.dto.*;
import com.geilizu.dom.entity.Moveout;
import com.geilizu.dom.dao.MoveoutDao;
import com.geilizu.dom.service.MoveoutService;
import com.geilizu.dom.util.CommonUtil;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Moveout)表服务实现类
 *
 * @author makejava
 * @since 2022-07-12 23:40:24
 */
@Service("moveoutService")
public class MoveoutServiceImpl implements MoveoutService {
    @Resource
    private MoveoutDao moveoutDao;
    @Resource
    private StudentDao studentDao;
    @Resource
    private DormitoryDao dormitoryDao;
    @Override
    public Map<String, Object> moveoutList(Integer page, Integer size) {
        page = (page - 1) * size;
        List<MoveoutDto> moveoutList = moveoutDao.moveoutList(page, size);
        Long count = moveoutDao.queryAllCount();
        Map<String, Object> map = new HashMap<>();
        map.put("moveoutList", moveoutList);
        map.put("count", count);
        return map;

    }

    @Override
    public Map<String, Object> search(SearchDto searchDto) {
        Integer page = (searchDto.getPage() - 1) * searchDto.getSize();
//        System.out.println(page+"---------------");
        List<MoveoutDto> absentList = new ArrayList<>();
        Long count = Long.valueOf(0);
        if (searchDto.getValue().equals("")) {
            absentList = moveoutDao.moveoutList(page, searchDto.getSize());
            count = moveoutDao.queryAllCount();
        } else if (searchDto.getKey().equals("studentName")) {
            SearchByName search = new SearchByName(page, searchDto.getSize(), "", "", searchDto.getValue());
            absentList = moveoutDao.queryByName(search);
            count = moveoutDao.queryByNameCount(search);
        } else if (searchDto.getKey().equals("dormitoryName")) {
            SearchByName search = new SearchByName(page, searchDto.getSize(), "", searchDto.getValue(), "");
            absentList = moveoutDao.queryByName(search);
            count = moveoutDao.queryByNameCount(search);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("moveoutList", absentList);
        map.put("count", count);
        return map;

    }

    @Override
    public Map<String, Object> list(Integer page, Integer size) {
        page = (page - 1) * size;
        List<StudentDto> moveList = moveoutDao.moveoList(page, size);
        Long count = moveoutDao.queryStuMoveCount();
        Map<String, Object> map = new HashMap<>();
        map.put("moveList", moveList);
        map.put("count", count);
        return map;

    }

    @Override
    public Map<String, Object> stuSearch(SearchDto searchDto) {

        Integer page = (searchDto.getPage() - 1) * searchDto.getSize();
//        System.out.println(page+"---------------");
        List<StudentDto> studentDtoList = new ArrayList<>();
        Long count = Long.valueOf(0);
        if (searchDto.getValue().equals("")) {
            studentDtoList = moveoutDao.moveoList(page, searchDto.getSize());
            count = moveoutDao.queryStuMoveCount();
        } else if (searchDto.getKey().equals("name")) {
            SearchStuDto stuDto = new SearchStuDto(page, searchDto.getSize(), searchDto.getValue(), "");
            studentDtoList = moveoutDao.queryStu(stuDto);
            count = moveoutDao.queryStuCount(stuDto);
        } else if (searchDto.getKey().equals("number")) {
            SearchStuDto stuDto = new SearchStuDto(page, searchDto.getSize(), "", searchDto.getValue());
            studentDtoList = moveoutDao.queryStu(stuDto);
            count = moveoutDao.queryStuCount(stuDto);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("stuList", studentDtoList);
        map.put("count", count);
        return map;

    }

    @Override
    public Integer moveout(Integer id, String reason) {
        Integer doId = studentDao.queryDoId(id);
        Moveout moveout = new Moveout();
        moveout.setStudentId(String.valueOf(id));
        moveout.setDormitoryId(String.valueOf(doId));
        moveout.setReason(reason);
        moveout.setCreateDate(CommonUtil.createDate());
        Integer integer = moveoutDao.insert(moveout);
        if (integer > 0) {
            Integer i = moveoutDao.updateState(id);
            dormitoryDao.updateAviableAdd(doId);
            return i;
        } else {
            return 0;
        }
    }

    @Override
    public Integer deleteById(Integer id) {
        return moveoutDao.deleteById(id);
    }
}
